// SampleOpenCVApp.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv/cv.h"
//#include "opencv/highgui.h"

void PrintHelp();
void ScaleImage(double factor, const char *pathfilenameImage);

int main(int argc, char* argv[])
{	
	using namespace std;

	cout << "***SampleOpenCVApp v1.0.0.1 by Will***"<< endl << endl;

	if (argc != 4 )
	{
		cout << "error: invalid args" << endl;
		PrintHelp();
	}
	else
	{
		string op(argv[1]);
		string factor(argv[2]);
		string pathfilenameImage(argv[3]);

		cout << op << " " << factor << " " <<  pathfilenameImage << endl;

		if ( op == "scale" )
		{
			ScaleImage(atof(factor.c_str()), pathfilenameImage.c_str());
		}
		else
		{
			cout << "error: operator '" << op << "' is not yet implemented" << endl;
			PrintHelp();
		}
	}

	cout << endl << "***ImageProcOpenCVApp ends ok***"<< endl << endl;

	return 0;
}

void PrintHelp()
{
	using namespace std;

	cout << "run from cmd.exe as:" << endl << "   SampleOpenCVApp scale 2 imagefile.jpg" << endl;
	cout << "where:" << endl;
	cout << "   'scale' is scaling operator to be applied to the image" << endl; 
	cout << "   '2' is scaling factor for scaling operator in range -5 to 5" << endl; 
	cout << "   'imagefile.jpg' is the path and filename of the image" << endl; 
}

void ScaleImage(double factor, const char *pathfilenameImage)
{
	using namespace cv;

	Mat outp;
	Mat inp = imread(pathfilenameImage);

	if (factor >= 0 )
		resize(inp, outp, Size(), factor, factor);
	else
		resize(inp, outp, Size(), 1/factor, 1/factor);

	namedWindow("Resized Image", CV_WINDOW_AUTOSIZE);
	imshow("Resized Image", outp);
	imwrite("output.png", outp);
	
	waitKey(0);
	destroyWindow("Resized Image");
	inp.release();
}

